package com.liyunc.demo.controller;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.enums.ResultCode;
import com.liyunc.demo.pojo.UserInfo;
import com.liyunc.demo.result.CommonResult;
import com.liyunc.demo.service.UserInfoService;
import com.liyunc.demo.util.ValidateCodeUtil;
import com.liyunc.demo.validate.ImageCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-21
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-21               liyuncong        Created
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/v1/auth")
public class AuthController {

    @Autowired
    private UserInfoService userInfoService;

    private final SessionStrategy session = new HttpSessionSessionStrategy();

    /**
     * 验证码生成.
     */
    @RequestMapping(value = "/image", method = RequestMethod.GET)
    public void validateCode(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        //利用图片生成工具生成图片
        ImageCode image = ValidateCodeUtil.createImage();
        //将验证码存在session中
        session.setAttribute(
            new ServletWebRequest(request),
            ValidateCodeUtil.SESSION_KEY_IMAGE_CODE,
            image);

        //将图片输出给浏览器
        //设置内容类型
        response.setContentType("image/png");
        OutputStream outputStream = response.getOutputStream();
        ImageIO.write(image.getImage(), "png", outputStream);
    }


    /**
     * register.
     */
    @PostMapping(value = "/register", produces = {MediaType.APPLICATION_JSON_VALUE})
    public CommonResult<ResultCode> register(UserInfo userInfo) {
        UserInfo user = userInfoService.register(userInfo);
        if (user != null) {
            //注册成功
            log.info("用户注册成功，用户信息为{}", user);
            return CommonResult.success(ResultCode.SUCCESS);
        } else {
            //注册成功
            log.info("用户注册失败，用户信息未保存");
            return CommonResult.failed(ResultCode.FAILED.getCode(), "");
        }
    }
}
